<template>
  <el-row>
    <el-col :span="11">
      <el-breadcrumb separator="/" style="margin: 0 0 20px 0">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>专家</el-breadcrumb-item>
      </el-breadcrumb>
      <el-input placeholder="请输入名称" size="small" style="width: 40%" v-model.trim="sousuo"></el-input>
      <el-button type="primary" size="small" @click="sousuoid()">搜索</el-button>
    </el-col>
    <el-col :span="11">
      <h1>专家</h1>
    </el-col>
    <el-col :span="2">
      <el-button type="primary" size="small" @click="dialogFormAdd = true">添加专家</el-button>
    </el-col>
    <el-col :span="24" style="margin:  10px 0 3% 0">
      <el-table
        :data="etData"
        style="width: 100%;">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="number"
          label="专家ID">
        </el-table-column>
        <el-table-column
          prop="name"
          label="专家名称">
        </el-table-column>
        <el-table-column
          prop="title"
          label="专家职称">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="lookex(scope.row)" >查看</el-button>
            <el-button type="text" size="small" @click="lookex1(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="delex(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-col>

    <el-col :span="24" style="text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage4"
        :page-sizes="[5,10]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
      <el-dialog title="查看专家" :visible.sync="dialogFormLook">
        <el-form :model="formLook" style="width: 80%">
          <el-form-item label="专家名称 :" label-width="20%">
            <el-input v-model="formLook.name"></el-input>
          </el-form-item>
          <el-form-item label="职务说明 :" label-width="20%">
            <el-input v-model="formLook.title"></el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="formLook.image" :src="formLook.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormLook = false">取 消</el-button>
          <el-button type="primary" @click="dialogFormLook = false">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="修改专家" :visible.sync="dialogFormUp">
        <el-form :model="formUp" style="width: 80%" :rules="rules1" ref="formUp">
          <el-form-item label="专家名称 :" label-width="20%" prop="name">
            <el-input v-model="formUp.name"></el-input>
          </el-form-item>
          <el-form-item label="职务说明 :" label-width="20%" prop="title">
            <el-input v-model="formUp.title"></el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess1"
              :before-upload="beforeAvatarUpload">
              <img v-if="formUp.image" :src="formUp.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm1('formUp')">取 消</el-button>
          <el-button type="primary" @click="upex('formUp')">确 定</el-button>
        </div>
      </el-dialog>
      <el-dialog title="添加专家" :visible.sync="dialogFormAdd">
        <el-form :model="formAdd" style="width: 80%" :rules="rules1" ref="formAdd">
          <el-form-item label="专家名称 :" label-width="20%" prop="name">
            <el-input v-model="formAdd.name"></el-input>
          </el-form-item>
          <el-form-item label="职务说明 :" label-width="20%" prop="title">
            <el-input v-model="formAdd.title"></el-input>
          </el-form-item>
          <el-form-item label="上传头像 :" label-width="20%">
            <el-upload
              class="avatar-uploader"
              action="https://xcx.wzlzfc.com/bbdj/house/uploadimage"
              :show-file-list="false"
              :on-success="handleAvatarSuccess2"
              :before-upload="beforeAvatarUpload">
              <img v-if="formAdd.image" :src="formAdd.image" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" style="line-height: 178px;"></i>
            </el-upload>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="resetForm('formAdd')">取 消</el-button>
          <el-button type="primary" @click="addet('formAdd')">确 定</el-button>
        </div>
      </el-dialog>
    </el-col>
  </el-row>
</template>

<script>
  import {getetlist,addexpert,delexpert,getexpertid,upexpert,etqueryid} from "@/api/expertApi";

  export default {
    data() {
      return {
        dialogFormUp: false,
        dialogFormAdd: false,
        dialogFormLook: false,
        currentPage4: 1,
        page:1,
        size:5,
        total:10,
        exid:'',
        sousuo:'',
        formUp: {
          name: '',
          title:'',
          image:'',
        },
        formAdd: {
          name: '',
          title:'',
          image:'',
        },
        formLook: {
          name: '',
          title:'',
          image:'',
        },
        etData: [{
          name: '',
          title:'',
        }],
        rules1: {
          name: [
            {required: true, message: '请输入专家名称', trigger: 'blur'},
          ],
          title: [
            {required: true, message: '请输入专家职称', trigger: 'blur'},
          ],
        }
      }
    },
    methods: {
      getList(){
        let data={
          page:this.page,
          size:this.size
        };
        this.etData=[];
        getetlist(data).then((res)=>{
          if(this.sousuo!==''){
            let data={number:this.sousuo};
            etqueryid(data).then((res)=>{
              if(res.data.data!=null){
                let a=res.data.data;
                this.etData.push(a);
              }
            })
          }else{
            this.etData=res.data.data;
          }
          this.total=res.data.data.length;
        })
      },
      addet(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data=this.formAdd;
            addexpert(data).then((res)=>{
              this.getList();
              this.resetForm(formName);
              this.$message({
                message: '添加专家成功!',
                type: 'success'
              });
            })
          } else {
            this.$message({
              message: '请填写完整!',
              type: 'warning'
            });
            return false;
          }
        });

      },
      delex(row){
        let data={
          customermanagerid:row.id
        };
        delexpert(data).then((res)=>{
          this.getList();
          this.$message({
            message: '删除专家成功!',
            type: 'success'
          });
        })
      },
      lookex(row){
        let data={
          customermanagerid:row.id
        };
        getexpertid(data).then((res)=>{
          this.formLook=res.data.data;
          this.dialogFormLook =true;
        })

      },
      lookex1(row){
        this.exid=row.id;
        let data={
          customermanagerid:row.id
        };
        getexpertid(data).then((res)=>{
          this.formUp=res.data.data;
          this.dialogFormUp =true;
        })
      },
      upex(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let id={
              customermanagerid:this.exid
            };
            let data=this.formUp;
            upexpert(id,data).then((res)=>{
              this.getList();
              this.resetForm(formName);
              this.$message({
                message: '修改经纪人成功!',
                type: 'success'
              });
            })
          } else {
            this.$message({
              message: '请填写完整!',
              type: 'warning'
            });
            return false;
          }
        });
      },
      sousuoid(){
        this.getList();
      },

      handleSizeChange(val) {
      },
      handleCurrentChange(val) {
      },
      handleAvatarSuccess(res, file) {
        this.formLook.image = res.data;
      },
      handleAvatarSuccess1(res, file) {
        this.formUp.image = res.data;
      },
      handleAvatarSuccess2(res, file) {
        this.formAdd.image = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      resetForm(formName) {
        this.dialogFormAdd = false;
        this.formAdd.image='';
        this.$refs[formName].resetFields();
      },
      resetForm1(formName) {
        this.dialogFormUp = false;
        this.formUp.image='';
        this.$refs[formName].resetFields();
      },
    },
    mounted(){
      this.getList();
    },

  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;

    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
